package org.minproject.my_library_back.controller;


import org.minproject.my_library_back.common.CommonResponse;
import org.minproject.my_library_back.domain.Book;
import org.minproject.my_library_back.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;
    //获取并展示所有书籍
    @GetMapping
    public CommonResponse<List<Book>> getAllBooks() {
        List<Book> bookList = bookService.selectAllBooks();
        boolean temp=bookList.isEmpty();
        if (temp) {
            String message = "暂无书籍";
            return  CommonResponse.createForSuccessMessage(message);

        }
        else {
            String message = "查询成功";
            return  CommonResponse.createForSuccess(message,bookList);
        }
    }
    //添加书籍
    @RequestMapping("/add")
    public CommonResponse<Book> addBook(@RequestBody Book book) {
        if (bookService.addBook(book)==1) {
            String message = "添加成功";
            return  CommonResponse.createForSuccessMessage(message);
        }
        else {
            String message = "添加失败";
            return  CommonResponse.createForError(500,message);
        }
    }
    //     删除书籍
    @DeleteMapping("/delete/{bookId}")
    public CommonResponse<Book> deleteBook(@RequestParam Integer bookId) {
        if (bookService.deleteBook(bookId)==1) {
            String message = "删除成功";
            return  CommonResponse.createForSuccessMessage(message);
        }
        else {
            String message = "删除失败";
            return  CommonResponse.createForError(500,message);
        }

    }
    //     更新书籍
    @PutMapping("/update")
    public CommonResponse<Book> updateBook(@RequestBody Book book) {
        if (bookService.updateBook(book)==1) {
            String message = "更新成功";
            return  CommonResponse.createForSuccessMessage(message);
        }
        else {
            String message = "更新失败";
            return  CommonResponse.createForError(500,message);
        }
    }
    //根据书名查询
    @GetMapping("/find/{bookName}")
    public CommonResponse<List<Book>> searchBookByName(@RequestParam String bookName) {
        List<Book> bookList = bookService.selectBookByName(bookName);
        boolean temp=bookList.isEmpty();
        if (temp) {
            String message = "暂无书籍";
            return  CommonResponse.createForSuccessMessage(message);
        }
        else {
            String message = "查询成功";
            return  CommonResponse.createForSuccess(bookList);
        }
    }
    //根据id查询
    @GetMapping("/{bookId}")
    public CommonResponse<Book> searchBookById(@PathVariable Integer bookId) {
        Book book = bookService.selectBookById(bookId);
        if (book == null) {
            String message = "暂无书籍";
            return  CommonResponse.createForSuccessMessage(message);
        }
        else {
            String message = "查询成功";
            return  CommonResponse.createForSuccess(book);
        }
    }
}
